# Maintainer: Fabien Chouteau <chouteau@adacore.com>

_realname=libadalang
pkgbase=mingw-w64-${_realname}
pkgname=${MINGW_PACKAGE_PREFIX}-${_realname}
pkgver=25.0.0
pkgrel=2
pkgdesc="Library for parsing and semantic analysis of Ada code (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64')
url="https://github.com/AdaCore/libadalang"
msys2_references=(
  'aur: libadalang'
  'gentoo: dev-ada/libadalang'
)
license=('spdx:Apache-2.0 WITH LLVM-exception')
depends=("${MINGW_PACKAGE_PREFIX}-adasat"
         "${MINGW_PACKAGE_PREFIX}-gcc-ada"
         "${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-gmp"
         "${MINGW_PACKAGE_PREFIX}-gnatcoll-core"
         "${MINGW_PACKAGE_PREFIX}-gnatcoll-gmp"
         "${MINGW_PACKAGE_PREFIX}-gnatcoll-iconv"
         "${MINGW_PACKAGE_PREFIX}-gpr"
         "${MINGW_PACKAGE_PREFIX}-langkit_support"
         "${MINGW_PACKAGE_PREFIX}-libgpr"
         "${MINGW_PACKAGE_PREFIX}-xmlada")
makedepends=("${MINGW_PACKAGE_PREFIX}-gprbuild"
             "${MINGW_PACKAGE_PREFIX}-langkit"
             "${MINGW_PACKAGE_PREFIX}-liblktlang"
             "${MINGW_PACKAGE_PREFIX}-python-build"
             "${MINGW_PACKAGE_PREFIX}-python-docutils"
             "${MINGW_PACKAGE_PREFIX}-python-e3-core"
             "${MINGW_PACKAGE_PREFIX}-python-funcy"
             "${MINGW_PACKAGE_PREFIX}-python-installer"
             "${MINGW_PACKAGE_PREFIX}-python-mako"
             "${MINGW_PACKAGE_PREFIX}-python-setuptools"
             "${MINGW_PACKAGE_PREFIX}-python-sphinx")
options=('!strip')
source=("https://github.com/AdaCore/libadalang/archive/v${pkgver}/${_realname}-${pkgver}.tar.gz")
sha256sums=('98b4f5ade46c51aa68e4c4eff28db7e36019e98be4bf88eebd728f0ba2d54c0d')

_library_types="static,static-pic,relocatable"
_build_mode="prod"

build() {
  [[ -d "${srcdir}"/build-${MSYSTEM} ]] && rm -rf "${srcdir}"/build-${MSYSTEM}
  cp -r "${srcdir}/${_realname}-${pkgver}" "${srcdir}"/build-${MSYSTEM}
  cd "${srcdir}"/build-${MSYSTEM}

  ${MINGW_PREFIX}/bin/python manage.py generate

  ${MINGW_PREFIX}/bin/python manage.py build \
    --library-types=${_library_types} \
    --build-mode=${_build_mode} \
    --disable-java \
    --gargs="-R"

  cd build/python
  ${MINGW_PREFIX}/bin/python -m build --wheel --skip-dependency-check --no-isolation
}

package() {
  cd "${srcdir}"/build-${MSYSTEM}

  # Install the Ada library with its C binding
  ${MINGW_PREFIX}/bin/python manage.py install \
      --library-types=${_library_types} \
      --build-mode=${_build_mode} \
      "$pkgdir/${MINGW_PREFIX}"

  # Install the Python binding
  cd build/python
  MSYS2_ARG_CONV_EXCL="--prefix=" \
    ${MINGW_PREFIX}/bin/python -m installer --prefix=${MINGW_PREFIX} \
    --destdir="${pkgdir}" dist/*.whl

  install -Dm644 -t "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}" \
    "${srcdir}/${_realname}-${pkgver}/LICENSE.txt"
}
